/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import POJO.POJO_GaTau;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Master
 */
public class DAO_GaTau {
    DataProvider data;
    
     public List<POJO_GaTau> LoadList() throws SQLException {
        data = new DataProvider();
        data.Open();
        
        String query = "SELECT * FROM ga_tau";
        List<POJO_GaTau> list = new ArrayList<>();
        ResultSet rs = data.ExecuteQuery(query);
        
        while (rs.next()) {
            POJO_GaTau dt = new POJO_GaTau();

            dt.setMAGA(rs.getString("MaGa"));
            dt.setTENGA(rs.getString("TenGa"));

            list.add(dt); 
        }
        return list;
    } 
    
    public int xoaGaTau(POJO_GaTau dt) {
            String sql = String.format("DELETE FROM ga_tau WHERE MaGa='%s'", dt.getMAGA());
            data = new DataProvider();
            data.Open();
            
            return data.ExeCuteUpdate(sql);
    }
    
    public int themGaTau(POJO_GaTau dt) {
            String sql = String.format("Insert into ga_tau values ('%s','%s')",
                    dt.getMAGA(),
                    dt.getTENGA());
            data = new DataProvider();
            data.Open();
            
            return data.ExeCuteUpdate(sql);
    } 
    
    public int capNhatGaTau(POJO_GaTau dt)  {
            String sql = String.format("UPDATE ga_tau SET TenGa = '%s' where MaGa = '%s'",                    
                dt.getTENGA(),
                dt.getMAGA());
            data = new DataProvider();
            data.Open();
            
            return data.ExeCuteUpdate(sql);
    }
}
